package com.ucar.ui.cheapcode;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONObject;

import com.google.gson.Gson;
import com.ucar.R;
import com.ucar.bean.Cheapcode;
import com.ucar.bean.Cheapcode.Result;
import com.ucar.net.BaseResponse;
import com.ucar.net.Callback;
import com.ucar.net.NetRequest;
import com.ucar.ui.base.BaseActivity;
import com.ucar.util.CacheUtils;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

public class CheapCodeActivity extends BaseActivity implements OnClickListener {

	private EditText et_cheapcode;
	private Button btn_ok;
	private ListView lv_cheapcode;
	private String token;
	private String cheapCode;
	private List<Result> result;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setBaseTitleContent(R.layout.activity_cheapcode, R.string.my_cheap_code);
	}

	@Override
	public void onInit() {
		setLeftImage(R.drawable.icon_arrow);
	}

	@Override
	public void onFindViews() {
		et_cheapcode = find(R.id.et_cheapcode);
		btn_ok = find(R.id.btn_ok);
		lv_cheapcode = find(R.id.lv_cheapcode);

	}

	@Override
	public void onInitViewData() {
		initListview();
	}

	private void initListview() {
		// 初始化下方的优惠码listview列表
		NetRequest.getInstance(this).get(new Callback() {

			@Override
			public void onPreExecute() {
				toShowProgressMsg(getString(R.string.loading));
			}

			@Override
			public void onFinished() {
				toCloseProgressMsg();
			}

			@Override
			public void onSuccess(BaseResponse data) {
				JSONObject jsonObject = data.getData();
				String response = data.response;
				// 存储到缓存数据中
				CacheUtils.setString(CheapCodeActivity.this, "jsonResponse",
						response);
				Gson gson = new Gson();
				Cheapcode cheapcode = gson.fromJson(response, Cheapcode.class);
				result = cheapcode.result;
				
				lv_cheapcode.setAdapter(new MyAdapter());

			}

			@Override
			public void onFailed(String msg) {
				showToast(msg);
			}

			@Override
			public Map<String, String> getParams() {
				token = mSpUtil.getToken();
				Map<String, String> params = new HashMap<String, String>();
				params.put("url", "app/getusersprod");
				params.put("token", token);
				return params;
			}
		});
	}

	@Override
	public void onBindListener() {
		btn_ok.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_ok:
			cheapCode = et_cheapcode.getText().toString().trim();
			if (TextUtils.isEmpty(cheapCode)) {
				showToast(R.string.input_cheap_code);
				return;
			}
			// 将输入的优惠码提交到服务器，获取到返回的信息，将得到的信息显示在下方的listview中，刷新listview 显示在第一行
			getCheapCodeInfo();
			break;

		default:
			break;
		}
	}

	private void getCheapCodeInfo() {
		String tag = "tag_get_cheapcode";
		NetRequest.getInstance(this).get(new Callback() {
			@Override
			public void onPreExecute() {
				toShowProgressMsg(getString(R.string.add));
			}

			@Override
			public void onSuccess(BaseResponse data) {
				showToast(R.string.addSuccess);
				// 刷新listview
				onInitViewData();
			}

			@Override
			public void onFinished() {
				toCloseProgressMsg();
			}

			@Override
			public void onFailed(String msg) {
				showToast(msg);
			}

			@Override
			public Map<String, String> getParams() {
				token = mSpUtil.getToken();
				cheapCode = et_cheapcode.getText().toString().trim();
				Map<String, String> params = new HashMap<String, String>();
				params.put("url", "app/verifycoupon");
				params.put("coupon", cheapCode);
				params.put("token", token);
				return params;
			}
		}, tag);
	}

	class MyAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			if (result != null) {
				return result.size();
			}
			return 0;
		}

		@Override
		public Object getItem(int position) {
			if (result != null) {
				return result.get(position);
			}
			return null;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder = null;
			if (convertView == null) {
				// 没有复用
				holder = new ViewHolder();
				convertView = View.inflate(CheapCodeActivity.this,
						R.layout.cheapcode_item, null);
				// 设置标记
				convertView.setTag(holder);

				// view的初始化
				holder.tv_cheap_price = (TextView) convertView
						.findViewById(R.id.tv_cheap_price);
				holder.tv_cheap_notes = (TextView) convertView
						.findViewById(R.id.tv_cheap_notes);
				holder.tv_expiredate_date = (TextView) convertView
						.findViewById(R.id.tv_expiredate_date);
				holder.tv_cheap_prodname = (TextView) convertView
						.findViewById(R.id.tv_cheap_prodname);
				holder.tv_cheapcode_num = (TextView) convertView
						.findViewById(R.id.tv_cheapcode_num);

			} else {
				holder = (ViewHolder) convertView.getTag();
			}

			// 给View设置数据
			Result bean = result.get(position);

			holder.tv_cheap_price.setText(bean.price+"元");
			holder.tv_cheap_notes.setText(bean.notes);
			holder.tv_expiredate_date.setText(bean.expiredate);
			holder.tv_cheap_prodname.setText(bean.prodname);
			holder.tv_cheapcode_num.setText(bean.qty+"张");

			// 设置颜色

			// 去读缓存

			return convertView;
		}
	}

	class ViewHolder {
		TextView tv_cheap_price;
		TextView tv_cheap_notes;
		TextView tv_expiredate_date;
		TextView tv_cheap_prodname;
		TextView tv_cheapcode_num;
	}
}
